﻿
Imports std
Imports tcpServer

Public Module nettransDataInserter
    Private Sub initialNettransData(ByRef d As nettransData, ByVal clearOriginalData As Boolean)
        If d Is Nothing Then
            d = New nettransData()
        ElseIf clearOriginalData Then
            d.clear()
        End If
    End Sub

    Public Function keyvaluepair(ByRef data As nettransData, _
                                 ByVal clearOriginalData As Boolean, _
                                 ByVal ParamArray ps() As String) As Boolean
        If ps Is Nothing OrElse (ps.Length() And 1) = 1 Then
            Return False
        End If
        initialNettransData(data, clearOriginalData)

        For i As Int64 = 0 To ps.Length() - 1 Step 2
            data.insert(ps(i))
            data.insert(ps(i + 1))
        Next

        Return True
    End Function

    Public Function keyvaluepair(ByRef data As nettransData, _
                                 ByVal ParamArray ps() As String) As Boolean
        Return keyvaluepair(data, True, ps)
    End Function

    Public Function keyvaluepair(ByRef data As nettransData, _
                                 ByVal pairs() As pair(Of String, String), _
                                 Optional ByVal clearOriginalData As Boolean = True) As Boolean
        assert(Not pairs Is Nothing, "pairs is nothing when insert nettransData")
        initialNettransData(data, clearOriginalData)

        Dim i As Int64
        For i = 0 To pairs.Length() - 1
            data.insert(pairs(i).first)
            data.insert(pairs(i).second)
        Next

        Return True
    End Function

    Public Function keyvaluepair(ByRef data As nettransData, _
                                 ByVal pairs As vector(Of pair(Of String, String)), _
                                 Optional ByVal clearOriginalData As Boolean = True) As Boolean
        assert(Not pairs Is Nothing, "pairs is nothing when insert nettransData")
        Return keyvaluepair(data, pairs.data(), clearOriginalData)
    End Function

    Public Function keyvaluepair(ByRef data As nettransData, _
                                 ByVal pairs As map(Of String, String), _
                                 Optional ByVal clearOriginalData As Boolean = True) As Boolean
        assert(Not pairs Is Nothing, "pairs is nothing when insert nettransData")
        initialNettransData(data, clearOriginalData)

        Dim it As map(Of String, String).iterator = Nothing
        it = pairs.begin()
        While Not it = pairs.end()
            data.insert((+it).first)
            data.insert((+it).second)
            it += 1
        End While

        Return True
    End Function

    Private Function prepareNettransData(ByVal ParamArray ps() As String) As nettransData
        Dim rtn As nettransData = Nothing
        If keyvaluepair(rtn, ps) Then
            Return rtn
        Else
            raiseError("failed to create keyvalue pairs nettransData.", errorHandle.errorType.exclamation)
            Return nettransData.emptyInstance()
        End If
    End Function

    Public Sub keyvaluepair(ByVal handle As talkHandle, ByVal ParamArray ps() As String)
        assert(Not handle Is Nothing)
        handle.return(prepareNettransData(ps))
    End Sub
End Module
